package com.common.www.utils;

import com.common.www.MyApplicationListener;
import com.common.www.bean.entity.Result;
import com.common.www.data.NetCode;

import org.springframework.data.domain.Page;

public class R {

    public static Result getResult(int code, String msg, Object data, Integer page, Integer totalPage, Long totalElements) {
        Result result = new Result();
        result.setMessage(msg);
        result.setData(data);
        result.setCode(code);
        result.setPage(page);
        result.setTotalPage(totalPage);
        result.setTotalElements(totalElements);
//        System.out.println(result.toString());

        if (!MyApplicationListener.isRelease) {//非发布模式下所有接口等待1秒后再返回模式网络时长
            try {
                Thread.sleep(250);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static Result ok(Object data, Integer page, Integer totalPage, Long totalElements) {
        return ok(data, NetCode.SUCCESS.reasonPhraseCN(), page, totalPage, totalElements);
    }

    public static Result ok(Object data, String msg, Integer page, Integer totalPage, Long totalElements) {
        return getResult(NetCode.SUCCESS.code(), msg, data, page, totalPage, totalElements);
    }

    public static Result returnCode(Object data, NetCode code, Integer page, Integer totalPage, Long totalElements) {
        return getResult(code.code(), code.reasonPhraseCN(), data, page, totalPage, totalElements);
    }

    public static Result returnCode(Object data, NetCode code) {
        return returnCode(data, code, 0, 0, 0L);
    }

    public static Result returnCode(NetCode code) {
        return returnCode(null, code, 0, 0, 0L);
    }

    public static Result ok(String msg) {
        return ok(null, msg);
    }

    public static Result ok(Object data) {
        return ok(data, 0, 0, 0L);
    }

    public static Result ok() {
        return ok(null, 0, 0, 0L);
    }

    public static Result ok(Object data, Page page) {
        return ok(data, page.getNumber(), page.getTotalPages(), page.getTotalElements());
    }

    public static Result ok(Page page) {
        return ok(page.getContent(), page);
    }

    public static Result ok(String msg, Object data) {
        return ok(data, msg, 0, 0, 0L);
    }

    public static Result fail(Object data, String msg, Integer page, Integer totalPage, Long totalElements) {
        return getResult(NetCode.NOT_IMPLEMENTED.code(), msg, data, page, totalPage, totalElements);
    }

    public static Result fail(int code, Object data, String msg) {
        return getResult(code, msg, data, 0, 0, 0L);
    }

    public static Result fail(int code, String msg) {
        return fail(code, null, msg);
    }

    public static Result fail(Object data, Integer page, Integer totalPage, Long totalElements) {
        return fail(data, NetCode.NOT_IMPLEMENTED.reasonPhraseCN(), page, totalPage, totalElements);
    }

    public static Result fail(Object data, String msg) {
        return fail(data, msg, 0, 0, 0L);
    }

    public static Result fail(String msg) {
        return fail(null, msg, 0, 0, 0L);
    }

    public static Result fail(NetCode code) {
        return fail(null, code.reasonPhraseCN(), 0, 0, 0L);
    }

    public static Result returnFailByLock() {
        return returnCode(NetCode.CODE_REDIS_LOCK);
    }
}
